package com.etcn.common.aop;

import ch.qos.logback.classic.Logger;
import com.etcn.common.utils.JsonHandler;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * aop日志
 * 
 * @author: guozc
 * @date: 2014年3月27日 上午8:49:38
 */
@Aspect
@Component
public class LogAspect {

	/**
	 * 业务处理 logger
	 */
	private Logger serviceLogger = (Logger) LoggerFactory.getLogger("service_file_log");

	/**
	 * 异常logger
	 */
	private Logger errorLogger = (Logger) LoggerFactory.getLogger("error_file_log");

	private JsonHandler jsonHandler;

	public LogAspect(){
		jsonHandler = JsonHandler.buildNoNull();
	}

	@Pointcut(value = "execution(* com..*.service.*.*(..))")
	public void pointCuts() {
	}

	/**
	 * 前置方法
	 * 
	 * @param joinPoint
	 * @return: void
	 */
	@Before(value = "pointCuts()")
	public void before(JoinPoint joinPoint) {
		serviceLogger.info("-------->" + joinPoint.getTarget().getClass() + " : " + joinPoint.getSignature().getName()
				+ "  begin  -------------------");
	}

	/**
	 * 后续方法
	 * 
	 * @param joinPoint
	 * @return: void
	 */
	@After(value = "pointCuts()")
	public void after(JoinPoint joinPoint) {
		serviceLogger.info("-------->" + joinPoint.getTarget().getClass() + " : " + joinPoint.getSignature().getName()
				+ "  end  -------------------");
	}

	/**
	 * 异常处理
	 * @param joinPoint
	 * @param ex
	 * @return: void
	 */
	@AfterThrowing(value = "pointCuts()", throwing = "ex")
	public void doThrowing (JoinPoint joinPoint,Throwable ex) {
		try {
			errorLogger.error("param  json  :  "+ jsonHandler.toJson(joinPoint.getArgs()));
		} catch (Exception e) {
			errorLogger.error("param json parse error",e);
		}
	}

}
